using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace RemoveMine.Entities
{
    // khoi tao thuoc tinh cua van choi va quan choi
    public enum TTVanChoi { ChuaBatDau, BatDau, Thua, Thang}
    class VanChoiDTO
    {
        private DoKhoDTO _dokho;

        public DoKhoDTO Dokho
        {
            get { return _dokho; }
            set { _dokho = value; }
        }
        private OcoDTO[,] _cacOco;

        internal OcoDTO[,] CacOco
        {
            get { return _cacOco; }
            set { _cacOco = value; }
        }

        private TTVanChoi _trangthai;
        public TTVanChoi TrangThai
        {
            get { return _trangthai; }
            set { _trangthai = value; }
        }

        private int _socodung;
        public int Socodung
        {
            get { return _socodung; }
            set { _socodung = value; }
        }

        private void KhoiTaoCacOCo()
        {
            int i, j, _dem;
            Point _toado;
            Random _random = new Random();           
           
            _cacOco = new OcoDTO[_dokho.Hang, _dokho.Cot];

            for(i = 0; i < _dokho.Hang; i++)
                for (j = 0; j < _dokho.Cot; j++)
                {
                    _toado = new Point(j, i);
                    //_cacOco[i, j] = new OcoDTO(_toado ,GiaTriOCo );
                    _cacOco[i, j] = new OcoDTO(_toado,GiaTriOCo .Zero , HanhDong.ChuaMo);
                }

            
            for (_dem = 0; _dem < _dokho.Somin; _dem++)
            {
                i = _random.Next(_dokho.Hang); j = _random.Next(_dokho.Cot);
                if (_cacOco[i, j].Giatri  == GiaTriOCo .Bom )
                    _dem--; 
                else 
                    _cacOco[i, j].Giatri = GiaTriOCo .Bom;
            }

            
            for (i = 0; i < _dokho.Hang; i++)
                for (j = 0; j < _dokho.Cot; j++)
                {
                    _dem = 0;
                    if (_cacOco[i,j].Giatri != GiaTriOCo .Bom)
                    {
                        int dx, dy;
                        for(dy = -1; dy <= 1; dy++)
                            for(dx = -1; dx <= 1; dx++)
                            {
                                int _hang, _cot;
                                _hang = i + dy; _cot = j + dx;
                                if ((_hang >= 0 && _hang < _dokho.Hang) && (_cot >= 0 && _cot < _dokho.Cot) 
                                    && (_cacOco[_hang, _cot].Giatri  ==  GiaTriOCo .Bom))                             
                                    _dem++;
                            }
                        _cacOco[i, j].Giatri  = (GiaTriOCo )_dem;
                    }
                }
                
        }
        public VanChoiDTO(DoKhoDTO dokho)
        {
            _dokho = dokho;
            KhoiTaoCacOCo();
        }
        public VanChoiDTO(DoKhoDTO dokho, TTVanChoi trangthai, int socodung)
        {
            _dokho = dokho;
            _trangthai = trangthai;
            _socodung = socodung;
            KhoiTaoCacOCo();
        }
    }
}
